<?php
	if (isset($_REQUEST['text2convert']) && ($_REQUEST['text2convert'] != "")) 
	{
		$content = htmlentities($_REQUEST['text2convert']);

		$lines = preg_split("/\\r\\n|\\r|\\n/", $content);
		
		$cnt = count($lines);
		
		$result = "\r";
		$last_indent = 0;
		for ($i = 0; $i < $cnt; $i++)
		{
			$firstChar = $lines[$i][0];
			
			if (($firstChar != "!")&&($firstChar != " ")&&($lines[$i] != "end"))
			{
				$fuck = $lines[$i];
				$result .= "sendln '$fuck'\r\n";
				$result .= "wait '#'\r\n";
				$last_indent = 0;
			}
			else if ($firstChar == " ")
			{
				$secondChar = $lines[$i][1];
				if (($secondChar != "!")&&(strpos($lines[$i],'vlan add default') == false)
					 && (strpos($lines[$i],'vlan pvid') == false))
				{
					$result .= "sendln '$lines[$i]'\r\n";
					$result .= "wait '#'\r\n";
				}
				$last_indent = 1;
			}
			else if($firstChar == "!")
			{
				if ($last_indent == 1)
				{
					$result .= "sendln 'exit'\r\n";
					$result .= "wait '#'\r\n";
				}
				$last_indent = 0;
			}
		}
	}	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
		<link rel="icon" type="image/png" href="images/icon.png"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Console Server Management</title>
		<script src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/jquery-latest.min.js"></script>
		<script type="text/javascript" src="js/script.js"></script>
        <link href="css/style.css" rel="stylesheet" type="text/css" />
        <style type="text/css"></style>
    </head>
	
<body>
        <div class="header">
			<div id="logo"></div>
			<div class="bubble">
				<p>CONSOLE SERVER MANAGEMENT</p>
			</div>	
		</div>

		<div id="cover">
		    <div id="cover_background">
				<div class="btn_navi">
					<div class="btn_config_macro">
						<form method="POST" action="config_macro.php">
							<input type="submit" id="config_macro" value="Config Macro" />
						</form>
					</div>

					<div class="btn_home">
						<form method="POST" action="index.php">
							<input type="submit" id="home" value="Home" />
						</form>
					</div>
					
				</div>
				<div id="howto_macro">
					<p>►The <b><i>Config Macro</i></b> helps you make the Tera Team macro for restoring the switch configuration easily. Just copy the output and save it to a *.ttl file.</p>				
				</div>	
			</div>
		</div>
		
        <div id="container">
			<form method="POST" action="config_macro.php" class="cfg_macro">								
				<textarea name="text2convert" id="inputBox"></textarea>
				<input type="submit" name="convert2macro" value="" id="btn_convert" />		
				<textarea name="tex_box" id="outputBox" readonly="readonly" onclick="this.focus();this.select()">
					<?php 
						if (isset($result))
							echo $result;
						else
							echo "\rThe output will be displayed here..";
					?>
				</textarea>	
			</form>
		</div>
                
		</div> 
		<div id="footer">
			<div class="about">
				<div class="about-content">
					<p>Hanoi R&D Branch © 2013</p>
					<p><b>Version 1.2 | </b>Designed by: <b>quyenlv</b>, <b>vieph</b>, <b>nghianv</b> and <b>anpt</b></p>
				</div>
			</div>
		</div>
    
</body>
</html>
